package com.moji.controller.web;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.ObjectUtil;
import com.moji.common.base.ApiResult;
import com.moji.common.entity.MyPage;
import com.moji.entity.MjUser;
import com.moji.entity.SysUser;
import com.moji.entity.dto.*;
import com.moji.entity.vo.MjUserVo;
import com.moji.entity.vo.SysUserVo;
import com.moji.service.MjUserService;
import com.moji.service.SysUserService;
import com.moji.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 客户信息表 前端控制器
 * </p>
 *
 * @author ccl
 * @since 2024-08-17
 */
@Slf4j
@RestController
@RequestMapping("web/mjUser")
@Api(tags = "客户相关web接口", value = "客户相关web接口")
public class MjUserController {

    @Resource
    private MjUserService mjUserService;

    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("add")
    @ApiOperation("注册 用户类型 userType 0 - 个人用户 1 - 企业用户\"")
    public ApiResult add(@RequestBody AddMjUserDto dto){
        return ApiResult.ok(mjUserService.add(dto));
    }

//    @PostMapping("queryPage")
//    @ApiOperation("分页查询用户信息数据")
//    public ApiResult<MyPage<MjUserVo>> queryPage(@RequestBody QueryUserDto dto){
//        MyPage<MjUserVo> ccpSystemUserMyPage = mjUserService.queryByPage(dto);
//        return ApiResult.ok(ccpSystemUserMyPage);
//    }
//
//    @GetMapping("detail")
//    @ApiOperation(value = "详情",response = MjUserVo.class)
//    public ApiResult detail(Long id){
//        return ApiResult.ok(mjUserService.detail(id));
//    }

    @GetMapping("generateGraphicCode")
    @ApiOperation("生成图形验证码")
    public void generateGraphicCode(HttpServletResponse response) throws IOException {
        // 随机4位数字，可重复
        //RandomGenerator randomGenerator = new RandomGenerator("4444", 4);
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100,4,4);
        //lineCaptcha.setGenerator(randomGenerator);
        // 重新生成code
        lineCaptcha.createCode();
        log.info("图形验证码=========",lineCaptcha.getCode()+"");
        //放入redis缓存,有效时间3分钟
        redisTemplate.opsForValue().set(lineCaptcha.getCode(),lineCaptcha.getCode(), 15, TimeUnit.MINUTES);
        lineCaptcha.write(response.getOutputStream());
        // 关闭流
        response.getOutputStream().close();
    }


    @GetMapping("checkGraphicCode")
    @ApiOperation("校验图形验证码(点击下一步)")
    public ApiResult checkGraphicCode(String graphicCode) {
        if(ObjectUtil.isEmpty(graphicCode)){
            throw new RuntimeException("图形验证码不能为空,请核查!");
        }
        if(!redisTemplate.hasKey(graphicCode)){
            throw new RuntimeException("图形验证码错误,请核查!");
        }
//        String graphicCodeTem=redisTemplate.opsForValue().get(graphicCode)+"";
//        if(!graphicCode.equals(graphicCodeTem)){
//            throw new RuntimeException("图形验证码不正确,请核查!");
//        }
        return ApiResult.ok(true);
    }

    @GetMapping("getPhoneCode")
    @ApiOperation("获取手机验证码")
    public ApiResult getPhoneCode(String mobile) {
        return ApiResult.ok("1234");
    }

    @PostMapping("resetPassword")
    @ApiOperation("重置密码")
    public ApiResult resetPassword(@RequestBody ResetPasswordDto dto){
        return ApiResult.ok(mjUserService.resetPassword(dto));
    }

    @PostMapping("login")
    @ApiOperation("登录系统")
    public ApiResult login(@RequestBody ReqLoginDto loginReqDao){
        return ApiResult.ok(mjUserService.login(loginReqDao));
    }

    @PostMapping("logout")
    @ApiOperation("退出系统")
    public ApiResult logout(@RequestBody ReqLogoutDto reqLogoutDao, HttpServletRequest request){
        String loginId = reqLogoutDao.getLoginId();
        mjUserService.logout(loginId,request);
        return ApiResult.ok(ResultCode.SUCCESS);
    }
}

